package com.wsz.logistic.controller.admin;

import com.wsz.logistic.bean.Warehouse;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin/warehouse")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/list")
    public String list() {
        return "admin/warehouse/warehouse-list";
    }

    @PostMapping("/sync")
    @ResponseBody
    public R sync() {
        warehouseService.sync();
        return R.ok();
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Warehouse warehouse) {
        return warehouseService.getPage(page, limit, warehouse);
    }

    @GetMapping("/edit")
    public String edit(Integer warehouseId, Model model) {
        if (warehouseId != null) {
            model.addAttribute("warehouse", warehouseService.getById(warehouseId));
        }
        return "admin/warehouse/warehouse-update";
    }

    @PostMapping(value = {"/update", "/status"})
    @ResponseBody
    public R update(Warehouse warehouse) {
        warehouseService.saveOrUpdate(warehouse);
        return R.ok();
    }

    @DeleteMapping("/delete/{warehouseId}")
    @ResponseBody
    public R delete(@PathVariable int warehouseId) {
        warehouseService.deleteById(warehouseId);
        return R.ok();
    }
}
